package com.project.weimi.activity;

import org.xutils.x;

import com.project.weimi.R;
import com.project.weimi.application.AppManager;
import com.project.weimi.application.Constant;
import com.project.weimi.application.SystemBarTintManager;
import com.project.weimi.utils.NetUtils;
import com.project.weimi.utils.SharedPreferencesHelper;
import com.project.weimi.widget.CustomBar;
import com.project.weimi.widget.ToastMessage;
import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

@SuppressLint("InlinedApi")
public abstract class BaseActivity extends FragmentActivity implements OnClickListener{
	public static final String TAG = "BaseActivity";
	
	public SystemBarTintManager tintManager;
	public CustomBar bar;
	
	public SharedPreferencesHelper spHelper;
	public String memberId;
	public String baseNickName;
	public String baseSex;
	public String baseAge;
	public String baseAddress;
	public String baseSign;
	public String baseHeadImgPath;
	public String integrate;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		x.view().inject(this);
		// 将Activity添加到堆栈
		AppManager.getAppManager().addActivity(this);
		// 竖屏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		// 检查网络状态
		if(!NetUtils.isConnected(this)){
			ToastMessage.showToast(this, "网络不给力，请检查网络设置");
		}
		// 沉浸式状态栏
		setSystemBar();
		
		spHelper = new SharedPreferencesHelper(this, Constant.USER_INFO);
		if(null != spHelper){
			memberId = (String) spHelper.get("memberId", "");
			//Log.e("TAG", "onCreate===BaseActivity---"+memberId);
			baseNickName = (String) spHelper.get("nickName", "");
			baseSex = (String) spHelper.get("sex", "");
			baseAge = (String) spHelper.get("age", "");
			baseSign = (String) spHelper.get("sign", "");
			baseAddress = (String) spHelper.get("address", "");
			baseHeadImgPath = (String) spHelper.get("headImgPath", "");
			integrate = (String) spHelper.get("integrate", ""); 
		} else {
			spHelper = null;
			memberId = "";
			baseNickName = "";
			baseSex = "";
			baseAge = "";
			baseSign = "";
			baseAddress = "";
			baseHeadImgPath = "";
			integrate = "";
		}
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		spHelper = new SharedPreferencesHelper(this, Constant.USER_INFO);
		if(null != spHelper){
			memberId = (String) spHelper.get("memberId", "");
			//Log.e("TAG", "onCreate===BaseActivity---"+memberId);
			baseNickName = (String) spHelper.get("nickName", "");
			baseSex = (String) spHelper.get("sex", "");
			baseAge = (String) spHelper.get("age", "");
			baseSign = (String) spHelper.get("sign", "");
			baseAddress = (String) spHelper.get("address", "");
			baseHeadImgPath = (String) spHelper.get("headImgPath", "");
			integrate = (String) spHelper.get("integrate", ""); 
		} else {
			spHelper = null;
			memberId = "";
			baseNickName = "";
			baseSex = "";
			baseAge = "";
			baseSign = "";
			baseAddress = "";
			baseHeadImgPath = "";
			integrate = "";
		}
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		spHelper = new SharedPreferencesHelper(this, Constant.USER_INFO);
		if(null != spHelper){
			memberId = (String) spHelper.get("memberId", "");
			//Log.e("TAG", "onCreate===BaseActivity---"+memberId);
			baseNickName = (String) spHelper.get("nickName", "");
			baseSex = (String) spHelper.get("sex", "");
			baseAge = (String) spHelper.get("age", "");
			baseSign = (String) spHelper.get("sign", "");
			baseAddress = (String) spHelper.get("address", "");
			baseHeadImgPath = (String) spHelper.get("headImgPath", "");
			integrate = (String) spHelper.get("integrate", ""); 
		} else {
			spHelper = null;
			memberId = "";
			baseNickName = "";
			baseSex = "";
			baseAge = "";
			baseSign = "";
			baseAddress = "";
			baseHeadImgPath = "";
			integrate = "";
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		spHelper = new SharedPreferencesHelper(this, Constant.USER_INFO);
		if(null != spHelper){
			memberId = (String) spHelper.get("memberId", "");
			//Log.e("TAG", "onCreate===BaseActivity---"+memberId);
			baseNickName = (String) spHelper.get("nickName", "");
			baseSex = (String) spHelper.get("sex", "");
			baseAge = (String) spHelper.get("age", "");
			baseSign = (String) spHelper.get("sign", "");
			baseAddress = (String) spHelper.get("address", "");
			baseHeadImgPath = (String) spHelper.get("headImgPath", "");
			integrate = (String) spHelper.get("integrate", ""); 
		} else {
			spHelper = null;
			memberId = "";
			baseNickName = "";
			baseSex = "";
			baseAge = "";
			baseSign = "";
			baseAddress = "";
			baseHeadImgPath = "";
			integrate = "";
		}
	}
	
	// 只有左侧的返回图标和中间的标题
	protected void setContentViewWithBar(int layoutId,String titleName){
		LayoutInflater inflater = getLayoutInflater();
		ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.activity_base_titlebar, null);
		inflater.inflate(layoutId, vg);
		setContentView(vg);
		bar = (CustomBar)findViewById(R.id.custom_bar);
		bar.initBar(vg);
		bar.setTitleBar(titleName);
		bar.getLeftBar().setOnClickListener(this);
	}
	
	// 左侧的返回图标、中间的标题和右侧的文字全有
	protected void setContentViewWithBar(int layoutId,String titleName,
			boolean isShow,String rightStr){
		setContentViewWithBar(layoutId,titleName);
		if(isShow){
			bar.getRightBar().setVisibility(View.VISIBLE);
			bar.getRightBar().setText(rightStr);
//			bar.getRightBar().setOnClickListener(l);
		}else{
			bar.getRightBar().setVisibility(View.GONE);
		}
	}
	
	// 只有标题的页面
	protected void setTitleName(String titleName){
		bar.setTitleBar(titleName);
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.left_bar){
			//finish();
			AppManager.getAppManager().finishActivity(this);
		}
	}
	
	// 沉浸式状态栏设置
	protected void setSystemBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}

		tintManager = new SystemBarTintManager(this);
		tintManager.setStatusBarTintEnabled(true);
		//tintManager.setStatusBarTintResource(R.color.main_titleColor);
	}

	private void setTranslucentStatus(boolean on) {
		Window win = getWindow();
		WindowManager.LayoutParams winParams = win.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (on) {
			winParams.flags |= bits;
		} else {
			winParams.flags &= ~bits;
		}
		win.setAttributes(winParams);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		//AppManager.getAppManager().finishActivity(this);
	}

	/** 设置顶部任务栏的底色 */
	void setStatusBar(int statusBarColorRes){
		tintManager.setStatusBarTintResource(statusBarColorRes);
	}

}
